package cn.fxyz.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import cn.fxyz.base.impl.BaseDaoImpl;
import cn.fxyz.dao.FoodDao;
import cn.fxyz.entity.Food;

public class FoodDaoImpl extends BaseDaoImpl<Food> implements FoodDao {
	@Autowired
    /**
     * 查询dao层接口实现类
     * 进行动态拼接查询，拼接hql语句进行多条件查询
     */
	private SessionFactory sessionFactory;
	public List<Food> getFood(String id, String foodname, String foodtype) {
		// TODO Auto-generated method stub
		String hql="from Food where ";
		if(id!=null&&!("").equals(id)){
			hql=hql+"id="+id;
		}
		if(foodname!=null&&("").equals(foodname)){
			hql=hql+"foodname='"+foodname+"'";
		}
		if(foodtype!=null&&("").equals(foodtype)){
			hql=hql+"foodtype='"+foodtype+"'";
		}
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		List<Food> list=query.list();
		return list;
	}
	
	
	//增删改

	public void addFood(Food food) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(food);
	}
	
	
	public boolean deleteFood(String id) {
		// TODO Auto-generated method stub
		String hql="delete from Food where id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
	
	
	public boolean modifyFood(Food food) {
		// TODO Auto-generated method stub
		String hql="update Food set foodname=?,price=?,mprice=?,remark=?,img=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, food.getFoodname());
		query.setFloat(1, food.getPrice());
		query.setFloat(2, food.getMprice());
		query.setString(3, food.getRemark());
		query.setString(4, food.getImg());
		return query.executeUpdate()>0;
	}
	
	
	public List<Food> getAll() {
		// TODO Auto-generated method stub
		String hql="from Food";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	
	public Food getFood(String id) {
		// TODO Auto-generated method stub
		String hql="from Food where id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return (Food)query.uniqueResult();
	}
}
